package com.ycy.leetcode.link;

public class LongestCommonPrefix {


  static class ListNode {
    int val;
    ListNode next;

    public ListNode(int val) {
      this.val = val;
    }
  }

  public static void main(String[] args) {

    ListNode A = new ListNode(10);
    A.next = new ListNode(9);
    A.next.next = new ListNode(7);

    ListNode B = new ListNode(10);
    B.next = new ListNode(9);
    B.next.next = new ListNode(8);

    System.out.println(getCommonPrefix(A,B).val);

  }

  public static ListNode getCommonPrefix(ListNode A, ListNode B) {
    if (A == null || B == null) {
      return null;
    }
    ListNode a = A;
    ListNode b = B;
    ListNode com = null;
    while (a != null && b != null && a.val == b.val) {
      com = a;
      a = a.next;
      b = b.next;
    }
    return com;
  }

}
